/**
 * Copyright © 2015 digitalfondue (info@digitalfondue.ch)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nulldev.util.web.flexibleKit.jfvParse;

class TokenizerMarkupDeclarationState {

	static void handleMarkupDeclarationOpenState(Tokenizer tokenizer, ProcessedInputStream processedInputStream) {
		int[] chars = new int[7];

		chars[0] = processedInputStream.peekNextInputCharacter(1);
		chars[1] = processedInputStream.peekNextInputCharacter(2);

		if (chars[0] == Characters.HYPHEN_MINUS && chars[1] == Characters.HYPHEN_MINUS) {
			processedInputStream.consume(2);
			tokenizer.createNewCommentToken();
			tokenizer.setState(TokenizerState.COMMENT_START_STATE);
		} else {
			chars[2] = processedInputStream.peekNextInputCharacter(3);
			chars[3] = processedInputStream.peekNextInputCharacter(4);
			chars[4] = processedInputStream.peekNextInputCharacter(5);
			chars[5] = processedInputStream.peekNextInputCharacter(6);
			chars[6] = processedInputStream.peekNextInputCharacter(7);

			if (Common.matchCharsCaseInsensitiveDoctype(chars)) {
				processedInputStream.consume(7);
				tokenizer.setState(TokenizerState.DOCTYPE_STATE);

			} else if (tokenizer.getAdjustedCurrentNode() != null && //
					!Node.NAMESPACE_HTML.equals(tokenizer.getAdjustedCurrentNode().getNamespaceURI()) && //
					matchCDATACodePoints(chars)) {

				processedInputStream.consume(7);
				tokenizer.setState(TokenizerState.CDATA_SECTION_STATE);

			} else {
				tokenizer.emitParseErrorAndSetState(TokenizerState.BOGUS_COMMENT_STATE);
			}
		}
	}

	private static final char[] CDATA = "[CDATA[".toCharArray();

	private static boolean matchCDATACodePoints(int[] chars) {
		for (int i = 0; i < chars.length; i++) {
			if ((CDATA[i]) != chars[i]) {
				return false;
			}
		}
		return true;
	}
}
